// pages/myOrder/myOrder.js
const app = getApp()
var host = app.globalData.host;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: '',
        orders: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        var that = this
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    clientHeight: res.windowHeight - 100
                });
            }
        })
        console.log(e)
        var id = e.id;
        var status = e.status;
        this.setData({ currentTab: id });
        this.loadOrders(status);
    },
    loadOrders: function(orderStatus) {
        var page = this;
        var userId = wx.getStorageSync("userId");
        wx.request({
            url: host + '/api/order/getOrderList',
            method: 'GET',
            data: {
                userId: userId,
                orderStatus: orderStatus,
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                var orders = res.data.data;
                console.log(orders);
                page.setData({
                    orders: orders
                });
            }
        })
    },
    // 实现切换导航栏
    switchNav: function(e) {

        var that = this;
        var status = e.currentTarget.dataset.status;
        // console.log("currentTab的当前值为：" + this.data.currentTab)
        // console.log("current当前值为：" + e.target.dataset.current)
        if (this.data.currentTab == e.target.dataset.current) {

            return false;
        } else {
            that.setData({ currentTab: e.target.dataset.current });
            // that.setData({ tip: '' });
            // that.setData({ form_info: '' });
            // console.log("currentTab和current的当前值不相等，所以if为假，进入else语句，把current的当前值赋给currentTab，")
            // console.log("所以currentTab的当前值变为：" + this.data.currentTab)
            // console.log("currentTab和current的当前值相等,三目运算结果就为true，此时类名为select，正确切换对应的样式")

        }
        that.loadOrders(status);

    },

    toList: function() {
        wx.switchTab({
            url: '../index/index'
        })
    },
    toPay: function(e) {
        wx.redirectTo({
            url: '../orderDetail/orderDetail?orderId=' + e.currentTarget.id
        })
    },
    toBuy: function(e) {
        var goodsId = e.currentTarget.id;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + goodsId,
        })
    },
    deleteOrder: function(e) {
        var page = this;
        var id = e.currentTarget.id;
        var status = e.currentTarget.dataset.status;
        wx.request({
            url: host + '/api/order/deleteOrder',
            method: 'GET',
            data: {
                id: id
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {
                var code = res.data.code;
                if (code == '0000') {
                    wx.showToast({
                        title: '删除成功',
                        icon: 'success',
                        duration: 1000
                    })
                    page.loadOrders(status);
                }
            }
        })
    },
    stopMove: function() {
        return false;
    }

})